package org.karelun.rabbit.bridge.ui.view;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.views.properties.PropertySheetEntry;
import org.eclipse.ui.views.properties.PropertySheetPage;
import org.karelun.rabbit.bridge.TransactionAdapterFactoryEditingDomain;

public class ExtendedPropertySheetPage extends PropertySheetPage{
	
	private class LocateValueAction extends Action{
		public LocateValueAction(){
			setText("LocateValue");
			setToolTipText("Locate value");
		}
		
		@Override
		public void run() {
			List<Object> selection=new ArrayList<Object>();
			for(Object object:objectToSelect){
				selection.add(editingDomain.getWrapper(object));
			}
			setSelectionToViewer(selection);
		}
	}
	
	protected List<Object> objectToSelect=new ArrayList<Object>();
	
	protected TransactionAdapterFactoryEditingDomain editingDomain;
	
	protected IAction locateValueAction=new LocateValueAction();
	
	public ExtendedPropertySheetPage(TransactionAdapterFactoryEditingDomain editingDomain){
		this.editingDomain=editingDomain;
	}
	
	protected void setSelectionToViewer(List<?> selection){
		
	}
	
	@Override
	public void makeContributions(IMenuManager menuManager,
			IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
		super.makeContributions(menuManager, toolBarManager, statusLineManager);
		toolBarManager.add(locateValueAction);
	}
	
	@Override
	public void handleEntrySelection(ISelection selection) {
		super.handleEntrySelection(selection);
		objectToSelect.clear();
		if(!selection.isEmpty()&&selection instanceof IStructuredSelection){
			IStructuredSelection structuredSelection=(IStructuredSelection)selection;
			if(structuredSelection.size()==1){
				Object object = structuredSelection.getFirstElement();
				if(object instanceof PropertySheetEntry){
					PropertySheetEntry entry=(PropertySheetEntry)object;
					Object[] values = entry.getValues();
					if(values!=null){
						for(int i=0;i<values.length;i++){
							Object value=values[i];
							if(value instanceof IItemPropertySource){
								Object realValue = ((IItemPropertySource)value).getEditableValue(null);
								if(realValue instanceof Collection<?>){
									for(Object o:(Collection<?>)realValue){
										addObjectToSelect(o);
									}
								}else{
									addObjectToSelect(realValue);
								}
							}
						}
					}
				}
				
			}
		}
		locateValueAction.setEnabled(!objectToSelect.isEmpty());
	}

	protected void addObjectToSelect(Object object) {
		if(object instanceof EObject){
			objectToSelect.add(object);
		}
	}
}
